$(document).ready(function(){
	initSelectors();
	$("#uploadFileButton").click(function(event){
		$.ajaxFileUpload({
			url:'photoupload',
			secureuri:false,
			fileElementId:"uploadPhotoInput",
			dataType: 'json',
			success:function(data, status){
				if(data.successMessage != ""){
					try{
						$("#registerPhotoImg").attr("src", "photo/" + data.successMessage);
						$("#photoIdInput").val(data.successMessage);
					}catch(ex){
						alert(ex);
					}
				}
			}
		});
	});
	$("#submitButton").click(function(){
		if($("#emailInput").val().search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1){
			alert("Please input a correct Email.");
			$("#emailInput")[0].focus();
			return false;
		}
		if($("#passwordInput").val() == ""){
			alert("Please input the password.");
			$("#passwordInput")[0].focus();
			return false;
		}
		if($("#passwordInput").val() != $("#passwordConfirmInput").val()){
			alert("The two passwords you entered did not match.");
			$("#passwordInput")[0].focus();
			return false;
		}
		$.ajaxFileUpload({
			url:'photoupload',
			secureuri:false,
			fileElementId:"uploadPhotoInput",
			dataType: 'json',
			success:function(data, status){
				if(data.successMessage != ""){
					try{
						$("#registerPhotoImg").attr("src", "photo/" + data.successMessage);
						$("#photoIdInput").val(data.successMessage);
					}catch(ex){
						alert(ex);
					}
				}
				$.ajaxFileUpload({
					url:'cvupload',
					secureuri:false,
					fileElementId:"uploadCVInput",
					dataType: 'json',
					success:function(data, status){
						if(data.successMessage != ""){
							try{
								$("#cvIdInput").val(data.successMessage);
								$("#cvFileNameInput").val(data.bindKey);
							}catch(ex){
								alert(ex);
							}
						}
						var careerInterestsStr1 = $("#careerInterestsInput1").attr("checked") == "checked" ? $("#careerInterestsInput1").val() : "";
						var careerInterestsStr2 = $("#careerInterestsInput2").attr("checked") == "checked" ? $("#careerInterestsInput2").val() : "";
						var careerInterestsStr3 = $("#careerInterestsInput3").attr("checked") == "checked" ? $("#careerInterestsInput3").val() : "";
						var careerInterestsStr4 = $("#careerInterestsInput4").attr("checked") == "checked" ? $("#careerInterestsInput4OtherText").val() : "";
						$.post("profile/edit", {
							firstName:$("#firstNameInput").val(),
							lastName:$("#lastNameInput").val(),
							email:$("#emailInput").val(),
							password:$("#passwordInput").val(),
							college:$("#collegeInput").val(),
							graduateSchool:$("#graduateSchoolInput").val(),
							yearOfGraduation:$("#yearOfGraduationInput").val(),
							roleOfInterest:$("#roleOfInterestInput").val(),
							locationPreference:$("#locationPreferenceInput").val(),
							currentEmployer:$("#currentEmployerInput").val(),
							jobTitle:$("#jobTitleInput").val(),
							formerEmployer:$("#formerEmployerInput").val(),
							employerIndustry:$("#employerIndustryInput").val(),
							availability:$("#availabilityInput").val(),
							photoId:$("#photoIdInput").val(),
							investingBackground:$("#investingBackgroundInput").val(),
							cvId:$("#cvIdInput").val(),
							cvFileName:$("#cvFileNameInput").val(),
							careerInterests1:careerInterestsStr1,
							careerInterests2:careerInterestsStr2,
							careerInterests3:careerInterestsStr3,
							careerInterests4:careerInterestsStr4
						}, function(data){
							if(data.success){
								window.location.href="studentHome";
							}else{
								alert(data.errorMessage);
							}
						}, "json");
					}
				});
			}
		});
	});
});

function initSelectors(){
	$("#roleOfInterestInput").val($("#roleOfInterestInputCurrent").val());
	$("#employerIndustryInput").val($("#employerIndustryInputCurrent").val());
	$("#availabilityInput").val($("#availabilityInputCurrent").val());
}

function initCareerInterest(msg){
	var index = 1;
	for(index = 1; index <= 3; index++){
		if($("#careerInterestsInput" + index).val() == msg){
			$("#careerInterestsInput" + index).attr("checked", "checked");
			return;
		}
	}
	$("#careerInterestsInput4").attr("checked", "checked");
	$("#careerInterestsInput4OtherText").val(msg);
}